package com.supper.star.evaluate.controller;

import com.github.pagehelper.PageInfo;
import com.supper.star.evaluate.entity.SystemEntity;
import com.supper.star.evaluate.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@Controller
public class SystemController {

    @Autowired
    private SystemService systemService;

    @RequestMapping(value = {"/","","/page/{page}"})
    public String index(@PathVariable(value = "page", required = false) Integer page, Integer pageSize, HttpServletRequest request, Model model) {

        /**
         * 分页
         */
        if (page == null || page == 0) {
            page = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 6;
        }
        PageInfo<System> pageInfo = systemService.getPage(page, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("page",page);
        model.addAttribute("number",pageInfo.getPages());
        return "systemList";
    }
//    @RequestMapping(value = "/systemList",method = RequestMethod.GET)
////    public System systemList(String system,String position){
////        return ;
////
////    }

    /**
     * addSystem的Post请求 根据需要调用相同方法名的不同请求方式
     * @return addSystem
     */
    @RequestMapping(value = "/systemAdd", method = RequestMethod.POST, consumes = "application/json;charset=utf-8")
    @ResponseBody
    public Boolean addUser(@RequestBody SystemEntity system) {

        if (system == null) {
            return false;
        }else {
            System.out.println(system);
            return systemService.addSystem(system);
        }
    }

    /**
     * 更新System信息
     * @param system
     * @return Boolean
     */
    @RequestMapping(value = "/systemEdit",method = RequestMethod.POST)
    @ResponseBody
    public Boolean updateUser(@RequestBody SystemEntity system) {
        if (system == null) {
            return false;
        }
        return systemService.addSystem(system);
    }

    /**
     * 删除system
     * @param id
     * @return
     */
    @RequestMapping("/systemDelete")
    @ResponseBody
    public Boolean toDelete(Integer id) {
        if(id == null){
            return false;
        }
        return systemService.deleteSystem(id);
    }
}
